package com.zhao.rabbitmq.three;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import com.zhao.rabbitmq.utils.RabbitMQUtils;

/**
 * @author xiaoZhao
 * @date 2022/9/26
 * @describe
 */
public class Work03 {

    public static final String TASK_QUEUE_NAME = "ack_queue";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMQUtils.getChannel();
        System.out.println("C2等待消息处理，时间较长");

        DeliverCallback deliverCallback =( consumerTag,  message) ->{
            // 沉睡30秒
            try {
                Thread.sleep(30*1000);
                System.out.println("接收到的消息："+new String(message.getBody()));
                // 手动应答
                /**
                * 1.消息标识 tag
                 * 2.是否批量应答
                **/
                channel.basicAck(message.getEnvelope().getDeliveryTag(),false);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        };
        // 设置不公平分发
        int prefetchCount = 1;
        channel.basicQos(prefetchCount);

        // 手动应答
        boolean autoAck = false;
        channel.basicConsume(TASK_QUEUE_NAME, autoAck,deliverCallback,(consumerTag ->{
            System.out.println(consumerTag+"消费者取消消费");
        }));
    }
}
